<!--申请审批页-->
<template>
    <!-- 顶部条 -->
    <div style="height: 6vh;background-color:#fff;padding:10px 20px;">
        <span style="line-height:60px;font-size:20px;">审批列表</span>
    </div>
    <!-- 审批搜索卡片 -->
    <el-card style="margin:20px;height: 70px;">
        <el-form :inline="true">
            <el-form-item label="用车人">
                <!-- 因为我们这里只有一个表单项,提交这个表单项就相当于整个表单提交，我们需要阻止表单的默认提交,这样会刷新整个页面
                所以这里需要加上 .prevent修饰符来阻止敲回车而触发的提交整个表单刷新页面的情况 -->
                <el-input placeholder="请输入用车人" style="width:220px;"
                          v-model="search.username" @keydown.enter.prevent="loadAudit"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button @click="resetSearch">重置</el-button>
                <el-button type="primary" @click="loadAudit">查询</el-button>
            </el-form-item>
        </el-form>
    </el-card>

    <!-- 审批单主体 -->
    <el-card style="margin:20px;">
        <!--  审批状态项  -->
        <el-radio-group style="margin-bottom: 15px;" v-model="type" @change="loadAudit">
            <el-radio-button value="10" size="large">待我审核</el-radio-button>
            <el-radio-button value="20" size="large">待他人审核</el-radio-button>
            <el-radio-button value="30" size="large">已审核</el-radio-button>
            <el-radio-button value="40" size="large">驳回</el-radio-button>
        </el-radio-group>
        <!--  审批列表  -->
        <el-table :data="AuditArr">
            <el-table-column label="编号" prop="id" align="center" width="55" type="index"></el-table-column>
            <el-table-column label="用车人" prop="username" align="center" width="110"></el-table-column>
            <el-table-column label="开始时间" prop="startTime"  align="center"></el-table-column>
            <el-table-column label="结束时间" prop="endTime"  align="center"></el-table-column>
            <el-table-column label="用车事由" prop="reason"  align="center"></el-table-column>
            <el-table-column label="审批人" prop="auditUsernameList"  align="center"></el-table-column>
            <el-table-column label="出发地" prop="departureAddr"  align="center"></el-table-column>
            <el-table-column label="目的地" prop="destinationAddr"  align="center"></el-table-column>
            <el-table-column label="操作" width="100" align="center" v-if="type==10||type==40" :key="audit">
                <template #default="scope">
                    <!-- 审批10：在待我审核页签下显示，需要当前登录用户进行审批 -->
                    <el-button type="primary" link v-if="type==10" @click="auditing(scope.row.id)">审批</el-button>
                    <!-- 查看40：在驳回页签下显示，可以查看已驳回申请的驳回原因 -->
                    <el-button type="primary" link v-if="type==40" @click="auditing(scope.row.id)">查看</el-button>
                </template>
            </el-table-column>
        </el-table>
    </el-card>

    <!-- 审批/查看弹窗 -->
    <el-dialog :title="dialogTitle" v-model="auditDialogVisible">
        <el-descriptions direction="horizontal" :column="2" border>
            <el-descriptions-item label="用车人">{{ auditDialogData.username }}</el-descriptions-item>
            <el-descriptions-item label="用车事由">{{ auditDialogData.reason }}</el-descriptions-item>
            <el-descriptions-item label="使用开始时间">{{ auditDialogData.startTime }}</el-descriptions-item>
            <el-descriptions-item label="使用结束时间">{{ auditDialogData.endTime }}</el-descriptions-item>
            <el-descriptions-item label="车辆出发地">{{ auditDialogData.departureAddr }}</el-descriptions-item>
            <el-descriptions-item label="车辆目的地">{{ auditDialogData.destinationAddr }}</el-descriptions-item>
            <el-descriptions-item label="驾照图片">
                <img :src="BASE_URL + auditDialogData.imgUrl" style="width:150px;">
            </el-descriptions-item>
            <el-descriptions-item label="备注">{{ auditDialogData.remark }}</el-descriptions-item>
            <!--  驳回原因要在驳回页签点击查看才显示 -->
            <el-descriptions-item label="驳回原因" v-if="auditDialogData.auditStatus == 40">{{ auditDialogData.rejectReason }}</el-descriptions-item>
        </el-descriptions>
        <template #footer v-if="auditDialogData.auditStatus == 10">
            <el-button @click="auditDialogVisible=false">取消</el-button>
            <el-button type="primary" plain @click="rejectInnerDialogVisible=true">驳回</el-button>
            <el-button type="primary" @click="auditPass">通过</el-button>
        </template>
    </el-dialog>

    <!-- 驳回原因弹窗 -->
    <el-dialog title="驳回 查看" v-model="rejectInnerDialogVisible" style="margin-top: 37vh;" :before-close="rejectConfirm">
        <el-descriptions direction="horizontal" border>
            <el-descriptions-item label="驳回原因">
                <el-input placeholder="请输入驳回原因" v-model="rejectReason"></el-input>
            </el-descriptions-item>
        </el-descriptions>
        <template #footer>
            <el-button type="primary" plain @click="rejectCancel">取消</el-button>
            <el-button type="primary" @click="auditReject">确定</el-button>
        </template>
    </el-dialog>
</template>

<script setup>
import {onMounted, ref} from "vue";
import qs from "qs";
import axios from "axios";
import {ElMessage} from "element-plus";

//控制审批弹窗标题
const dialogTitle = ref("待审批详情");
//控制审批弹窗是否显示
const auditDialogVisible = ref(false);
//控制驳回原因弹窗是否显示
const rejectInnerDialogVisible = ref(false);

//定义数组用来保存审批单表格数据
const AuditArr = ref([]);
//定义审批单搜索卡片对象
const search = ref({
    username:'', //绑定搜索卡片中工单申请人姓名
    auditUserId:'', //预先准备 接下来要查属于当前用户的审批单
    auditStatus:'' //预先准备 查不同状态的审批单数据
});
//获取当前登录的用户数据
const user = ref(getUser());
//绑定上方审核单状态单选组，默认值为"待我审核"
const type = ref(10);

//定义加载数据函数
const loadAudit= ()=>{
    //只查询自己的审核单
    search.value.auditUserId = user.value.id;
    search.value.auditStatus = type.value;
    let data = qs.stringify(search.value);
    //根据上面 data 封装的条件，向服务器端请求对应的审批单数据
    axios.get(BASE_URL+'/v1/audit/select?'+data).then((response)=>{
        if(response.data.code == 2002){
            AuditArr.value = response.data.data;
        }
    })
}

//重置搜索
const resetSearch = ()=>{
    search.value.username = '';//清空用车人输入框
    loadAudit();//重新加载数据
}

//页面一加载立即加载数据
onMounted(()=>{
    loadAudit();
})
//定义对象保存审批弹窗数据
const auditDialogData = ref({
    username:'',
    reason:'',
    startTime:'',
    endTime:'',
    departureAddr:'',
    destinationAddr:'',
    imgUrl:'',
    remark:''
});
//点击审批按钮，弹出审批单详情弹窗
const auditing = (id)=>{
    //显示审批/驳回弹窗
    auditDialogVisible.value = true;
    axios.get(BASE_URL+'/v1/audit/select?id='+id)
        .then((res)=>{
            if (res.data.code == 2002){
                //此处将审批单的id存入auditDialogData中
                auditDialogData.value = res.data.data[0];
                console.log(auditDialogData.value)
            } else {
                ElMessage.error(res.data.msg)
            }
        })
}
//通过审批
const auditPass = ()=>{
    //将审批单状态修改为30
    auditDialogData.value.auditStatus = 30;
    let data = qs.stringify(auditDialogData.value);
    axios.post(BASE_URL+'/v1/audit/update',data)
        .then((res)=>{
            if (res.data.code === 2002){
                ElMessage.success("审批已通过");
                auditDialogVisible.value = false;
                //将审批单详情内容置空，便于下次查看详情时更新数据
                auditDialogData.value = {};
                loadAudit();
            } else {
                ElMessage.error(res.data.msg);
            }
        })
}

//定义变量保存驳回原因
const rejectReason = ref();
//驳回审批，点驳回弹窗确认时触发
const auditReject = ()=>{
    if (rejectReason.value == null){
        ElMessage.error("请输入驳回原因");
        return;
    }
    //关闭两层弹窗
    rejectInnerDialogVisible.value = false;
    auditDialogVisible.value = false;
    //将审批单状态修改为40——驳回
    auditDialogData.value.auditStatus = 40;
    auditDialogData.value.rejectReason = rejectReason.value;
    let data = qs.stringify(auditDialogData.value);
    axios.post(BASE_URL+'/v1/audit/update',data)
        .then((res)=>{
            if (res.data.code === 2002){
                ElMessage.success("审批已驳回");
                rejectReason.value='';
                auditDialogData.value={};
            }
            loadAudit();
        })
}
//关闭驳回弹窗操作
const rejectConfirm = ()=>{
    if (confirm("确认关闭吗？")){
        rejectReason.value='';
        rejectInnerDialogVisible.value = false;
    }
}
//取消驳回操作
const rejectCancel = ()=>{
    if (confirm("确认取消驳回吗？")){
        rejectReason.value='';
        rejectInnerDialogVisible.value = false;
        auditDialogVisible.value = false;
    }
}
</script>

<style>

</style>